<?php
namespace app\socketio\controller;

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use app\admin\model\ChatMessage;
use PHPSocketIO\SocketIO;
use Workerman\Redis\Client;

//聊天专用workerman
class Server
{
    public function index()
    {
        
        //证书最好是申请的证书
        $context = array(
            // 更多ssl选项请参考手册 http://php.net/manual/zh/context.ssl.php
            'ssl' => array(
                // 请使用绝对路径
                'local_cert'                 => '/www/wwwroot/yuxiang/public/uploads/oa.yuxiang123.cn.pem', // 也可以是crt文件
                'local_pk'                   => '/www/wwwroot/yuxiang/public/uploads/oa.yuxiang123.cn.key',
                'verify_peer'                => false,
                // 'allow_self_signed' => true, //如果是自签名证书需要开启此选项
            )
        );
        // 这里设置的是websocket协议（端口任意，但是需要保证没被其它程序占用）
        $worker = new Worker('websocket://0.0.0.0:3120', $context);
        // 设置transport开启ssl，websocket+ssl即wss
        $worker->transport = 'ssl';
        // 启动1个进程对外提供服务
        $worker->count = 1;
        // 当收到客户端发来的数据后返回hello $data给客户端
        // $worker->onMessage = function(TcpConnection $connection, $data){
        //     global $worker;
        //     //推送给发送人新消息
        //     $connection = $worker->data;
        //     $connection->send('sss---'.$data);
            
        // };
        // 新增加一个属性，用来保存uid到connection的映射
        $worker->uidConnections = array();
        // 当有客户端发来消息时执行的回调函数
        $worker->onMessage = function($connection, $data)
        {
            global $worker;
            if(!isset($connection->uid)){
                $connection->uid = $data;
                $worker->uidConnections[$connection->uid] = $connection;
            }else{
                foreach($worker->uidConnections as $k=>$connection){
                    if(strpos($data, "^") == false){
                        if($k == $data){
                            $connection->send('你好呀');
                        }
                    }else{
                        $arr = explode('^',$data);
                        $pos = strpos($arr[0], "group_chat");  //是否是群聊推送
                        if(count($arr) == 2){
                            $res = json_encode(['type'=>1,'data'=>json_decode($arr[1],true)]);
                            if($pos !== false){  //是群聊
                                if($k != $arr[0]){
                                    $connection->send($res);
                                }
                            }else{ // 是私聊
                                if($k == $arr[0]){
                                    $connection->send($res);
                                }
                            }
                        }elseif(count($arr) == 3){//撤回消息
                            $res = json_encode(['type'=>2,'data'=>$arr[2]]);
                            if($pos !== false){  //是群聊
                                if($k != $arr[0]){
                                    $connection->send($res);
                                }
                            }else{ // 是私聊
                                if($k == $arr[0]){
                                    $connection->send($res);
                                }
                            }
                        }
                    }

                }
            }
 
        };
        // 当有客户端连接断开时
        $worker->onClose = function($connection)
        {
            global $worker;
            if(isset($connection->uid))
            {
                // 连接断开时删除映射
                unset($worker->uidConnections[$connection->uid]);
            }
        };

        //运行worker
        Worker::runAll();
    }

}
